﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using Autobiography.Helpers;

namespace Autobiography.Design
{
    class DateGrouping : IEnumerable<IGrouping<String,Model.FileEntry>>
    {
        IEnumerable<IGrouping<String, Model.FileEntry>> dates = null;

        public DateGrouping()
        {
            var rand = new Random();
            dates = Enumerable
                .Range(1, 100)
                .Select(i => new Model.FileEntry { Modified = DateTime.Now.AddDays(rand.NextDouble() * -10.0) })
                .GroupBy(grouping)
                .OrderBy(sorting);
        }

        public static Func<Model.FileEntry, String> grouping = e =>
        {
            return e.Modified.Date == DateTime.Today ? "Today"
                : e.Modified.Date == DateTime.Today.AddDays(-1) ? "Yesterday"
                : e.Modified.Date >= DateTime.Today.FirstDayOfWeek() ? "This Week"
                : e.Modified.Date >= DateTime.Today.FirstDayOfWeek().AddDays(-1).FirstDayOfWeek() ? "Last Week"
                : "Older";
        };

        public static Func<IGrouping<String, Model.FileEntry>, int> sorting = g =>
        {
            switch (g.Key)
            {
                case "Today":
                    return 1;
                case "Yesterday":
                    return 2;
                case "This Week":
                    return 3;
                case "Last Week":
                    return 4;
                default:
                    return 100;
            }
        };

        public IEnumerator<IGrouping<string, Model.FileEntry>> GetEnumerator()
        {
            return dates.GetEnumerator();
        }

        System.Collections.IEnumerator System.Collections.IEnumerable.GetEnumerator()
        {
            return dates.GetEnumerator();
        }
    }
}
